package com.hanscan.persistence.controllers;


import com.hanscan.persistence.EntityManagerSingleton;
import com.hanscan.persistence.controllers.exceptions.NonexistentEntityException;
import com.hanscan.persistence.entities.BioTemplate;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;


public class BioTemplateJpaController implements Serializable
{

    public BioTemplateJpaController()
    {
    }

    public EntityManager getEntityManager()
    {
        return EntityManagerSingleton.getInstance();
    }

    public void create(BioTemplate bioTemplate) throws Exception
    {
        //TODO: comprobaciones
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(bioTemplate);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void edit(BioTemplate bioTemplate) throws NonexistentEntityException,
            Exception
    {
        //TODO: comprobaciones
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            bioTemplate = em.merge(bioTemplate);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void destroy(BioTemplate bioTemplate) throws Exception
    {
        //TODO: comprobaciones
        //TODO: si se borra usuario, se borran tb sus localizaciones, a no ser q el borrado sea logico
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            em.remove(bioTemplate);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public List<BioTemplate> findBioTemplateEntities()
    {
        return findBioTemplateEntities(true, -1, -1);
    }

    public List<BioTemplate> findBioTemplateEntities(int maxResults,
            int firstResult)
    {
        return findBioTemplateEntities(false, maxResults, firstResult);
    }

    private List<BioTemplate> findBioTemplateEntities(boolean all,
            int maxResults, int firstResult)
    {
        EntityManager em = getEntityManager();
        try
        {
            Query q = em.createQuery("select object(o) from BioTemplate as o");
            if (!all)
            {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        }
        finally
        {
            em.close();
        }
    }

    public BioTemplate findBioTemplate(Long id)
    {
        EntityManager em = getEntityManager();
        try
        {
            return em.find(BioTemplate.class, id);
        }
        finally
        {
            em.close();
        }
    }

    public int getBioTemplateCount()
    {
        EntityManager em = getEntityManager();
        try
        {
            Query q = em.createQuery("select count(o) from BioTemplate as o");
            return ((Long) q.getSingleResult()).intValue();
        }
        finally
        {
            em.close();
        }
    }
}
